package com.digitalchina.lxnsy.activity.management;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.management.message.MessageListActivity;
import com.digitalchina.lxnsy.activity.old.LoginGuideActivity;
import com.digitalchina.lxnsy.adapter.ManagementAdapter;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.PermissionListVO;
import com.digitalchina.lxnsy.toolkit.ActivityTaskManager;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.toolkit.SharedPreferencesUtil;
import com.digitalchina.lxnsy.xml.AdminXML;
import com.lzy.okhttputils.model.HttpParams;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity.management
 * 创建人：郑皓
 * 创建时间：2019/4/23 15:12
 * 修改备注：
 * ================================================
 */
public class ManagementDetailsActivity extends BaseActivity {

    @Bind(R.id.lv1)
    ListView lv1;
    @Bind(R.id.lv2)
    ListView lv2;

    private int index = 0;
    private ManagementAdapter adapter1, adapter2;

    private List<PermissionListVO> voList;

    String array1[] = {"社员管理", "农资经销存", "农事操作", "生产指导", "经营分析", "消息", "销售预估"};
    String array2[][] = {{"社员权限", "基础信息"},
            {"农用品采购", "种子采购", "农资出入库", "出库销售"},
            {"育苗", "定植", "浇水施肥", "中耕除草", "农药使用", "采收"},
            {"生产预测"},
            {"经营情况"},
            {"消息"},
            {"销售预估"}};

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_management_details);
        setTitle("农企（合作社）管理系统");
        if (AdminXML.getUserType(activity).equals("1"))
            setRightText("退出");
    }

    @Override
    public void initMember() {

        if (AdminXML.getUserType(activity).equals("1")) {
            // 管理员
            hideBackBtn();
        } else {
            // 普通用户
        }

        getUserPermission();

    }

    @Override
    public void addListener() {


        lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                index = i;
                adapter1.setSelectPosition(i);
                adapter2.setDatas(voList.get(i).getChildren());
            }
        });

        lv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                goActivity(adapter2.getItem(i));
            }
        });
    }

    // 页面跳转
    private void goActivity(PermissionListVO vo) {
        AdminXML.setResNo(activity, vo.getResNo());
        switch (vo.getName()) {
            case "社员权限":
                GotoUtil.gotoActivity(activity, MemberPermissionListActivity.class);
                break;
            case "基础信息":
                GotoUtil.gotoActivity(activity, BasicInfoActivity.class);
                break;
            case "农用品采购":
                GotoUtil.gotoActivity(activity, AddFarmProductActivity.class);
                break;
            case "种子采购":
                GotoUtil.gotoActivity(activity, AddSeedActivity.class);
                break;
            case "农资出入库":
                GotoUtil.gotoActivity(activity, AddOutInActivity.class);
                break;
            case "出库销售":
                GotoUtil.gotoActivity(activity, AddProductOutActivity.class);
                break;
            case "育苗":
                GotoUtil.gotoActivity(activity, GrowSeedActivity.class);
                break;
            case "定植":
                GotoUtil.gotoActivity(activity, PlantingActivity.class);
                break;
            case "浇水施肥":
                GotoUtil.gotoActivity(activity, WateringActivity.class);
                break;
            case "中耕除草":
                GotoUtil.gotoActivity(activity, WeedingActivity.class);
                break;
            case "农药使用":
                GotoUtil.gotoActivity(activity, PesticideActivity.class);
                break;
            case "采收":
                GotoUtil.gotoActivity(activity, HarvestActivity.class);
                break;
            case "生产预测":
                GotoUtil.gotoActivity(activity, ProductionForecastActivity.class);
                break;
            case "经营情况":
                GotoUtil.gotoActivity(activity, BusinessSituationActivity.class);
                break;
            case "消息":
                GotoUtil.gotoActivity(activity, MessageListActivity.class);
                break;
            case "销售预估":
                GotoUtil.gotoActivity(activity, SalesForecastActivity.class);
                break;
        }
    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case MSG_S_0:
                        voList = FastJsonUtil.getListBean(message.obj.toString(), "resourceList", PermissionListVO.class);
                        if (voList == null || voList.size() == 0)
                            return false;


                        adapter1 = new ManagementAdapter(activity, voList);
                        adapter1.setSelectPosition(index);
                        adapter2 = new ManagementAdapter(activity, voList.get(0).getChildren());
                        lv1.setAdapter(adapter1);
                        lv2.setAdapter(adapter2);
                        break;
                    case MSG_F_0:
                        showToast(message.obj.toString());
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {
        new MaterialDialog.Builder(activity)
                .title("提示")
                .content("您确定要退出登录吗？")
                .positiveText("确定")
                .negativeText("取消")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        SharedPreferencesUtil.cleanXml(activity, AdminXML.NAME);
                        go(LoginGuideActivity.class);
                        ActivityTaskManager.getInstance().closeAllActivity();
                    }
                })
                .show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private void getUserPermission() {

        HttpParams params = new HttpParams();
        params.put("enterpriseId", AdminXML.getEnterpriseId(activity));
        //params.put("resNo", AdminXML.getResNo(activity));
        params.put("userAccId", AdminXML.getUserAccId(activity));

        OkHttpUtil.post(activity, URL.URL_GET_USER_PERMISSION, "正在获取权限", params, mHandler,
                MSG_S_0, MSG_F_0);
    }
}
